<?php
/**
 * Punny - The most easy-to-use PHP MVC framework
 * 
 * Copyright (c) 2010  
 *
 * @author email :lx.xin@qq.com
 */
 require('BaseController.php');
 class Admin_LinkController extends Admin_BaseController {
	var $links ;
	function init(){
		parent::init();
		$this->links =new Link();
	}
	/*
	 *获取友情连接列表
	 */
	function indexAction (){
		$numPerPage = 10;
		$getPage = $this->_request->getParam('page');
		$page = empty($getPage)?'1':$getPage;
		//抓回所有要显示的记录
		$array=$this->links->getLink();
		//factory出一个分页并且设置当前页码与每页显数
		$paginator = Zend_Paginator::factory($array);
		$paginator->setCurrentPageNumber($page)
				  ->setItemCountPerPage($numPerPage);
         //把分页显示到view
		$this->view->paginator = $paginator;		
		echo $this->view->render('link.html');
	}

	/*
	 *添加友情链接
	 */
	 function addAction(){
		if(isset($_POST['add'])){
			if($this->_request->isPost()){
				$filter=array(
					'*'=>'StringTrim'	//	'*'=>'StringTrim'	
				);
				$validate=array(
					'name'=>array(
						array('StringLength',1,20),
						Zend_Filter_Input::MESSAGES=>array(array(
							Zend_Validate_StringLength::INVALID=>"请输入一个合法的字符串",
							Zend_Validate_StringLength::TOO_SHORT=>"请输入字符长度为1-20",
							Zend_Validate_StringLength::TOO_LONG=>"请输入字符长度为1-20"
					
				))
				),
					'ordernum'=>array('Int','message'=>array(
						Zend_Validate_Int::INVALID=>"请输入一个合法的字符",
						Zend_Validate_Int::NOT_INT=>"%value%不是一个数字"
				))
				);
				$input=new Zend_Filter_Input($filter,$validate,$this->getRequest()->getPost());
				if($input->isValid()){
					$url=$input->getUnknown('url');//这里返回的是一个数组，不能获取单个数组的值					
					if($this->links->check($url['url'])){
						$data=array_merge($input->getEscaped(),$input->getUnknown());
						unset($data['add']);
						isset($data['ishidden']) ? $data['ishidden']=1 : $data['ishidden']=0 ;
						isset($data['isapp']) ? $data['isapp']=1 : $data['isapp']=0 ;
						//var_dump($data);
						$this->links->addLink($data);
						$this->view->url=$this->_request->getBaseUrl()."/admin/link/index/";
						$this->view->msg="添加成功";
						echo  $this->view->render('success.html');
					}
					else{
						$this->view->url=$this->_request->getBaseUrl()."/admin/link/add/";
						$this->view->msg="网址已经存在";
						echo $this->view->render('error.html');
					}
				}
				else{
					$this->view->errors=$input->getMessages();
					$this->view->url=$this->_request->getBaseUrl()."/admin/link/add/";
					$this->view->msg="添加失败";
					echo $this->view->render('error.html');
				}
			}
		}
		else{
			$this->view->x='add';
			echo $this->view->render('link.html');
		}
	 }
	 /*
	  *编辑
	  */
	 function editAction(){
		if(isset($_POST['edit'])){
			$id=$this->_request->get('id');
			$data['name']=$this->_request->getPost('name');
			$data['url']=$this->_request->getPost('url');
			$data['ordernum']=$this->_request->getPost('ordernum');
			$this->_request->getPost('ishidden')=="on" ? $data['ishidden']=1 : $data['ishidden']=0 ;
			$this->_request->getPost('isapp')=="on" ? $data['isapp']=1 : $data['isapp']=0;
			$this->links->updateLink($id,$data);
			$this->view->url=$this->_request->getBaseUrl()."/admin/link/";
			$this->view->msg="编辑成功";
			echo $this->view->render('success.html');
		}
		else{
			$id=$this->_request->get('id');
			if($id>0){				
				$data=$this->links->queryLink($id);
				$this->view->id=$data['id'];
				$this->view->name=$data['name'];
				$this->view->url=$data['url'];
				$this->view->ordernum=$data['ordernum'];
				$this->view->ishidden=$data['ishidden'];
				$this->view->isapp=$data['isapp'];
				$this->view->x='edit';
				echo $this->view->render('link.html');
			}
			else{
				$this->view->url=$this->_request->getBaseUrl()."/admin/link/";
				$this->view->msg="要编辑的数据不存在";
				echo $this->view->render('error.html');
			}
		}
	 }
	 /*
	  *删除
	  */
	  function delAction(){
		$id=(int)$this->_request->get('id');
		if($id>0){
			$this->links->delLink($id);
			$this->view->url=$this->_request->getBaseUrl()."/admin/link/";
			$this->view->msg="删除成功";
			echo $this->view->render('success.html');
		}
		else{
			$this->view->url=$this->_request->getBaseUrl()."/admin/link/";
			$this->view->msg="要删除的数据不存在";
			echo $this->view->render('error.html');
		}
	  }
 }
 ?>